package Action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import Beans.Agent;
import Beans.Express;
import Beans.LogPay;
import DAO.AgentDao;
import DAO.BranchDao;
import DAO.ExpressDao;
import DAO.LogPayDao;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class AgentAction extends ActionSupport {

	private List<Agent> agents = new ArrayList<Agent>();
	private List<LogPay> logPays;
	private List<Express> expresses;

	AgentDao agentDao = new AgentDao();
	BranchDao branchDao = new BranchDao();
	ExpressDao expressDao = new ExpressDao();
	LogPayDao logPayDao = new LogPayDao();

	private int agentID;
	private String province;
	private String city;
	private Agent agent = new Agent();

	/**
	 * 编辑代理商
	 * 
	 * @return
	 */
	public String editAgent() {
		Agent a = agentDao.getAgentById(agentID);
		agent.setId(agentID);
		agent.setProvince(province);
		agent.setCity(city);
		agent.setBalance(a.getBalance());
		agentDao.edit(agent);
		return "success";
	}

	/**
	 * 删除代理商(分配终端则不可删除)
	 * 
	 * @return
	 */
	public String deleteAgent() {
		if (branchDao.getBranchByAgentId(agentID).size() > 0) {
		} else {
			agent.setId(agentID);
			agentDao.delete(agent);
		}
		return "success";
	}

	/**
	 * 新增代理商
	 * 
	 * @return
	 */
	public String registerAgent() {
		agent.setProvince(province);
		agent.setCity(city);
		agent.setBalance(new BigDecimal(0));
		agentDao.save(agent);
		return "success";
	}

	/**
	 * 通过ID获取代理商
	 */
	public String getAgentById() {
		agent = agentDao.getAgentById(agentID);
		return "success";
	}
	
	public String getByAgentID() {
		agent = agentDao.getAgentById(agentID);
		agents.add(agent);
		return "list";
	}

	/**
	 * 获取代理商流水账
	 * 
	 * @return
	 */
	public String getAgentBill() {
		agent = agentDao.getAgentById(agentID);
		expresses = expressDao.getAgentExpress(agent);
		logPays = logPayDao.getAllLogPay(expresses);
		return "bill";
	}

	/**
	 * 获取所有代理商
	 */
	public String getAllAgent() {
		agents = agentDao.getAllAgent();
		return "success";
	}
	/**
	 * 选择代理商
	 * @return
	 */
	public String selectAgent() {
		agents = agentDao.getAllAgent();
		return "selectAgent";
	}

	public List<Agent> getAgents() {
		return agents;
	}

	public void setAgents(List<Agent> agents) {
		this.agents = agents;
	}

	public int getAgentID() {
		return agentID;
	}

	public void setAgentID(int agentID) {
		this.agentID = agentID;
	}

	public Agent getAgent() {
		return agent;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	public List<Express> getExpresses() {
		return expresses;
	}

	public void setExpresses(List<Express> expresses) {
		this.expresses = expresses;
	}

	public List<LogPay> getLogPays() {
		return logPays;
	}

	public void setLogPays(List<LogPay> logPays) {
		this.logPays = logPays;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

}
